package com.tool.ui;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import com.tool.ResourceBundleHelper;

/**
 * UI基类
 * 
 * @author Evan.Hu
 * 
 */
public class CoreUI extends JPanel {

	private static final long serialVersionUID = -8517092259327806935L;
	private Window window = null;
	private Image iconImage = null;

	private JMenuBar menuBar = null;
	private String title = null;
	private boolean resizable = true;

	protected ResourceBundleHelper resHelper = null;

	public CoreUI() {
		try {
			BeautyEyeLNFHelper.launchBeautyEyeLNF();
			UIManager.put("RootPane.setupButtonVisible", false);
		} catch (Exception e) {
			e.printStackTrace();
		}

		setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getResource("tool.gif")));

	}

	protected void showUI() {
		JFrame f = createFrame();
		setWindow(f);
		f.setIconImage(getIconImage());
		f.setTitle(getTitle());
		f.setPreferredSize(getPreferredSize());
		f.setJMenuBar(getMenuBar());
		f.getContentPane().add(this);
		f.pack();

		try {
			onLoad();
		} catch (final Exception e) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					handUIException(e);
				}
			});
		}
		f.setResizable(isResizable());

		Rectangle screenRect = f.getGraphicsConfiguration().getBounds();
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(f.getGraphicsConfiguration());

		// Make sure we don't place the demo off the screen.
		int centerWidth = screenRect.width < f.getSize().width ? screenRect.x : screenRect.x + screenRect.width / 2 - f.getSize().width / 2;
		int centerHeight = screenRect.height < f.getSize().height ? screenRect.y : screenRect.y + screenRect.height / 2 - f.getSize().height / 2;

		centerHeight = centerHeight < screenInsets.top ? screenInsets.top : centerHeight;

		f.setLocation(centerWidth, centerHeight);

		f.show();

	}

	protected void onLoad() throws Exception {

	}

	public JFrame createFrame() {
		JFrame frame = new JFrame(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		WindowListener l = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				coreUIClosing(e);
			}
		};
		frame.addWindowListener(l);

		frame.setMinimumSize(new Dimension(100, 100));
		return frame;
	}

	protected void coreUIClosing(WindowEvent e) {

	}

	public static void main(String[] args) {

		CoreUI ui = new CoreUI();
		ui.showUI();
	}

	public void handUIException(Throwable exc) {
		// exc.printStackTrace();
		JOptionPane.showMessageDialog(getWindow(), exc.getMessage(), "", JOptionPane.WARNING_MESSAGE);
	}

	public static boolean isStringEmpty(String str) {
		return str == null || str.trim().length() == 0 ? true : false;
	}

	public Window getWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}

	public JMenuBar getMenuBar() {
		return menuBar;
	}

	public void setMenuBar(JMenuBar menuBar) {
		this.menuBar = menuBar;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public boolean isResizable() {
		return resizable;
	}

	public void setResizable(boolean resizable) {
		this.resizable = resizable;
	}

	public void setIconImage(Image iconImage) {
		this.iconImage = iconImage;
	}

	public Image getIconImage() {
		return iconImage;
	}
}
